﻿using System;
using biz.ritter.javapi.io;
using biz.ritter.javapi.lang;
using log4net;
using Nofs.Fuse;
using Nofs.Net.Execution;
using Nofs.Net.Fuse.Impl;
using NConsoler;

namespace Nofs.Net
{
    class Program
    {

        //static void Main(string[] args)
        //{
        //    Consolery.Run(typeof(Program), args);
        //}

        //[Action]
        //public static void Download
        //    (
        //    [Required] string institutionId,
        //    [Required] string suffix,
        //    [Required] string publishDir
        //    )
        //{
            
        //}
        

        static void Main(string[] args)
        {
            // Pull in log4net configuration
            log4net.Config.XmlConfigurator.Configure();

            ILog log = log4net.LogManager.GetLogger(typeof(Program));

            Arguments arg = new Arguments(args);

            if (!arg.ArgumentsValid())
            {
                arg.PrintUsage();
                SystemJ.exit(-1);

            }
            else
            {
                arg.PrintArguments();
                ExternalToInternalPathTranslator.SetMountPath(new File(arg.MountPoint).getAbsolutePath());
                String[] fuseArgs = new String[] 
                    {
					    //"-obig_writes",
					    "-f",
					    "-s",
					    arg.MountPoint
			        };

                NoFSFuseDriver fs = null;
                try
                {
                    fs = new NoFSFuseDriver(arg.DomainFile, arg.ObjectStore, arg.MetaStore, arg.MetadataDriver, arg.PersistenceDriver);
                    fs.Init();
                    FuseMount.mount(fuseArgs, fs, log);
                }
                catch (System.Exception e)
                {
                    log.Error(e);
                }
                finally
                {
                    if (fs != null)
                    {
                        fs.CleanUp();
                    }
                }
            }
        }


        private class Arguments
        {
            public String DomainFile;
            public String MountPoint;
            public String ObjectStore;
            public String MetaStore;
            public String MetadataDriver;
            public String PersistenceDriver;
            private bool _valid;

            public Arguments(String[] args)
            {
                if (args.Length == 6)
                {
                    _valid = true;
                    MetadataDriver = args[0];
                    PersistenceDriver = args[1];
                    DomainFile = args[2];
                    MountPoint = args[3];
                    ObjectStore = args[4];
                    MetaStore = args[5];
                }
                else
                {
                    _valid = false;
                }
            }

            public bool ArgumentsValid()
            {
                return _valid &&
                    new File(ObjectStore).exists() &&
                    new File(MountPoint).exists() &&
                    new File(DomainFile).exists();
            }

            public void PrintUsage()
            {
                Console.WriteLine("args: [metadata driver] [persistence driver] [domain file] [mount point] [object store] [metadata store]");
                if (!_valid)
                {
                    Console.WriteLine("argument count not valid.");
                }
                if (!new File(ObjectStore).exists())
                {
                    Console.WriteLine(ObjectStore + " does not exist.");
                }
                if (!new File(MountPoint).exists())
                {
                    Console.WriteLine(MountPoint + " does not exist");
                }
                if (!new File(DomainFile).exists())
                {
                    Console.WriteLine(DomainFile + " does not exist");
                }
            }

            public void PrintArguments()
            {
                Console.WriteLine("Metadata Driver    -> " + MetadataDriver);
                Console.WriteLine("Persistence Driver -> " + PersistenceDriver);
                Console.WriteLine("Domain File        -> " + DomainFile);
                Console.WriteLine("Mount Point        -> " + MountPoint);
                Console.WriteLine("Object Store       -> " + ObjectStore);
                Console.WriteLine("Metadata Store     -> " + MetaStore);
            }
        }
    }
}
